package com.yymmarket.forum.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yymmarket.forum.cache.ForumCache;
import com.yymmarket.forum.util.LevelUtil;
import com.yymmarket.util.Integer2;

public class AdminService {

	private static AdminService beanService;

	private AdminService() {
	}

	public static AdminService getInstance() {
		if (beanService == null) {
			synchronized (AdminService.class) {
				if (beanService == null)
					beanService = new AdminService();
			}
		}
		return beanService;
	}
	
	private AdminDAO adminDAO = new AdminDAO();
	
	static Map<Integer2,List<Integer>> adminListCache = ForumCache.adminListCache;
	static Map<Integer, AdminBean> adminCache = ForumCache.adminCache;
	
	public AdminBean getAdminById(int id) {
		
		AdminBean bean = adminCache.get(id);
		if(bean == null) {
			bean = this.adminDAO.getAdmin(id);
			if(bean != null)
				adminCache.put(id, bean);
		}
		
		return bean;
	}
	
	public AdminBean getAdmin(int type,int foreignerId,int userId) {
		List<AdminBean> list = getAdminList(type,foreignerId);
		for (AdminBean bean : list) {
			if(bean.getUserId()==userId) {
				return bean;
			}
		}
		return new AdminBean(foreignerId,LevelUtil.BBS_LEVEL_NORAML,userId);
	}
	
	public List<AdminBean> getAdminList(int type,int foreignerId) {
		Integer2 int2 = new Integer2(type,foreignerId);
		
		List<Integer> list = adminListCache.get(int2);
		
		if(list == null) {
			list = adminDAO.getAdmin(type, foreignerId);
			adminListCache.put(int2, list);
		}			
		//System.out.println(list.size());
		List<AdminBean> adminList = new ArrayList<AdminBean>();
		
		for (Integer integer : list) {
			//System.out.println(integer);
			AdminBean bean = this.getAdminById(integer);
			if(bean.getEndTime()==0||bean.getEndTime()>System.currentTimeMillis()) {
				adminList.add(bean);
			}
		}
		
		return adminList;
		
	}
	
	public List<AdminBean> getAdminByLevel(int type,int foreignerId,int level) {
		Integer2 int2 = new Integer2(type,foreignerId);
		
		List<Integer> list = adminListCache.get(int2);
		
		if(list == null) {
			list = adminDAO.getAdmin(type, foreignerId);
			adminListCache.put(int2, list);
		}			
		//System.out.println(list.size());
		List<AdminBean> adminList = new ArrayList<AdminBean>();
		
		for (Integer integer : list) {
			//System.out.println(integer);
			AdminBean bean = this.getAdminById(integer);
			if(bean.getEndTime()==0||bean.getEndTime()>System.currentTimeMillis()) {
				if(bean.getAdminLevel() >= level) {
					adminList.add(bean);
				}
			} else {
				this.deleteAdmin(type, foreignerId, bean.getUserId(), 0);
			}
		}
		return adminList;
	}
	
	public List<AdminBean> getAdminByLevelDesc(int type,int foreignerId,int level) {
		Integer2 int2 = new Integer2(type,foreignerId);
		
		List<Integer> list = adminListCache.get(int2);
		
		if(list == null) {
			list = adminDAO.getAdmin(type, foreignerId);
			adminListCache.put(int2, list);
		}			
		//System.out.println(list.size());
		List<AdminBean> adminList = new ArrayList<AdminBean>();
		
		for (Integer integer : list) {
			//System.out.println(integer);
			AdminBean bean = this.getAdminById(integer);
			if(bean.getEndTime()==0||bean.getEndTime()>System.currentTimeMillis()) {
				if(bean.getAdminLevel() <= level) {
					adminList.add(bean);
				}
			} else {
				this.deleteAdmin(type, foreignerId, bean.getUserId(), 0);
			}
		}
		return adminList;
	}
	
	
	public boolean addAdmin(AdminBean bean) {
		boolean flag = this.adminDAO.addAdmin(bean);
		if(flag) {
			Integer2 integer = new Integer2(bean.getType(),bean.getForeignerId());
			//List<Integer> list = adminListCache.get(integer);
		
			adminListCache.remove(integer);
			adminCache.put(bean.getId(), bean);
		}		
		return flag;
	}
	
	public boolean deleteAdmin(int type,int foreignerId, int uid,int deleteUserId) {
		Integer2 integer = new Integer2(type,foreignerId);
		List<Integer> list = adminListCache.get(integer);
		boolean flag = false;
		for (Integer integer2 : list) {
			AdminBean adminBean = this.getAdminById(integer2);
			if(adminBean !=null) {
				if(adminBean.getUserId()==uid) {
					flag = this.adminDAO.deleteAdminByIdAndDeleteUserId(integer2,deleteUserId);
					if(flag) {
						adminCache.remove(integer2);
					}
					break;
				}
			}
		}
		adminListCache.remove(integer);
		
		return flag;
	}
}
